/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.extns.bluetooth;

import java.util.Vector;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;

/**
 * Bluetooth radio utilities
 * 
 * @author kentelt
 * 
 */
public final class BTUtil implements DiscoveryListener {

	private Vector nearDevices;
	private boolean searchComplete;

	private void addDevice(RemoteDevice device) {
		nearDevices.addElement(new Device(device));
	}

	/**
	 * Cancel search for nearby devices
	 */
	private void cancelDeviceSearch() {
		LocalDevice localDevice;
		try {
			localDevice = LocalDevice.getLocalDevice();
			DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent();
			discoveryAgent.cancelInquiry(this);
		} catch (BluetoothStateException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Connect to a service
	 * 
	 * @param service
	 *            to connect to
	 * @param isMaster
	 *            of this connection
	 */
	public BTClient connect(Service service, boolean isMaster) {
		return new BTClient(service.getServiceRecord().getConnectionURL(0, isMaster));
	}

	public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
		addDevice(btDevice);
	}

	/**
	 * Call after searchComplete returns true.
	 * 
	 * @return
	 * @see searchComplete
	 */
	public Device[] getDevicesFound() {
		Device[] found = new Device[nearDevices.size()];
		nearDevices.copyInto(found);
		return found;
	}

	public void inquiryCompleted(int discType) {
		searchComplete = true;

	}

	/**
	 * Check to see if the search for nearby devices is complete
	 * 
	 * @return
	 */
	public boolean searchComplete() {
		return searchComplete;
	}

	public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {

	}

	public void serviceSearchCompleted(int transID, int respCode) {

	}

	/**
	 * Initiate a search for nearby devices
	 */
	public void startDeviceSearch() {
		searchComplete = false;
		nearDevices = new Vector();
		// Get the discovery agent
		LocalDevice localDevice;
		try {
			localDevice = LocalDevice.getLocalDevice();
			if (localDevice != null) {
				DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent();
				discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
			} else {
				System.out.println("[BTUtils]No Bluetooth hardware found");
			}
		} catch (BluetoothStateException e1) {

			e1.printStackTrace();
		}
		// try {
		// wait();
		// } catch (InterruptedException e) {
		// e.printStackTrace();
		// }
	}
}
